package org.example.innovate.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.example.innovate.common.CommonResponse;
import org.example.innovate.domain.User;
import org.example.innovate.dto.FollowDTO;
import org.example.innovate.dto.PageDTO;
import org.example.innovate.service.FollowService;
import org.example.innovate.service.UserService;
import org.example.innovate.util.JwtUtil;
import org.example.innovate.vo.UserAvatarVo;
import org.example.innovate.vo.UserVo;
import org.example.innovate.vo.uploadVo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.Map;

@RestController
@RequestMapping("/users")
@Valid
public class UserController {
    @Resource
    UserService usersService;
    @Resource
    FollowService followService;
    //登录
    @PostMapping("/login")
    public CommonResponse<String> login(@Validated @RequestBody User user) {
        return usersService.login(user);
    }

    //注册
    @PostMapping("/register")
    public CommonResponse<User> register(@Valid @RequestBody User user) {
        return usersService.register(user);
    }

    @GetMapping("/getUser")
    public CommonResponse<UserVo> getUser(HttpServletRequest request) {
        return usersService.getUser(request);
    }


    @PostMapping("/updateUser")
    public CommonResponse<UserVo> updateUser(@Valid @RequestBody User user, HttpServletRequest request) {
        return usersService.updateUser(user,request);
    }

    @PostMapping("/{userId}/follow")
    public CommonResponse<?> toggleFollow(
            @PathVariable("userId") Integer currentUserId, // 改为Integer类型
            @RequestBody String json) {

        JSONObject jsonObject = JSON.parseObject(json);
        FollowDTO followDTO=jsonObject.toJavaObject(FollowDTO.class);
        String action = followDTO.getAction();
        int followedUserId = followDTO.getUserId();
        try {
            if ("follow".equals(action)) {
                followService.follow(currentUserId, followedUserId);
            } else if ("unfollow".equals(action)) {
                followService.unfollow(currentUserId, followedUserId);
            } else {
                return CommonResponse.createForError("非法操作");
            }
            return CommonResponse.createForSuccess();
        } catch (RuntimeException e) {
            return CommonResponse.createForError(e.getMessage());
        }
    }


    @GetMapping("/{userId}")
    public CommonResponse<UserVo> getUserById(@PathVariable("userId") Integer userId) {
        return usersService.getUserById(userId);
    }

    @PostMapping("/upload/avatar")
    public CommonResponse<UserAvatarVo> upload(@RequestParam("image") MultipartFile image, HttpServletRequest request){
        return usersService.upload(image,request);
    }
}
